<script setup>
import { productlst, cate, bannerlist, productindex } from "@/services/index";
import { earnCount, skip } from "@/services/user";
import { onShow, onShareAppMessage, onShareTimeline } from "@dcloudio/uni-app";
import { nextTick, ref, watch, onMounted } from "vue";
import tabbarView from "../components/tabbar.vue";
import { useUserStore } from "../../stores/user";
import { userloginStore } from "@/stores/islogin.js";
import { date } from "uview-plus/libs/function/test";
import { formatPrice } from "@/utils/formatPrice";
import { goodsselectStore } from "@/stores/goods.js";

import imageurl from "../../static/index/home-1.png";
import imageur2 from "../../static/index/home-2.png";
import imageur3 from "../../static/index/home-3.png";
import imageur4 from "../../static/index/home-4.png";
// import { share } from "@/utils/share.js";
const shoplist = ref([
  {
    id: 1,
    name: "标准款",
    icon: imageurl,
  },
  {
    id: 2,
    name: "礼品装",
    icon: imageur2,
  },
  {
    id: 3,
    name: "灸具",
    icon: imageur3,
  },
  {
    id: 3,
    name: "特别款",
    icon: imageur3,
  },
  {
    id: 5,
    name: "特别款",
    icon: imageur4,
  },
]);

const banner = ref();
const content = ref();
const bannerarr = ref([]);
//#ifdef H5

uni.hideTabBar();
//#endif

const user = useUserStore();
const userlogin = userloginStore();
const cates = goodsselectStore();

// #ifdef H5
// share(
//   "经销商",
//   // encodeURIComponent(window.location.href.split("#")[0]),
//   window.location.href,
//   "https://lmt-app-cloud.yibaitao.com/20240420/a80079ef-050f-2f13-b6a3-82d0fa552d29.png"
// );
// #endif

const userinfo = ref(null);
const taoll = ref({});
const linkobj = ref({});
const ishowlin = ref(false);

async function getuserdatafun() {
  let userobj = await user.getuserInfo();
  userinfo.value = userobj;
}
const loding = ref(false);

bannerfun();
onShow(() => {
  valuetabbar.value = 0;
  ishowlin.value = false;
  earnCountfun();
  if (user.userInfo) {
    if (user.userInfo && user.userInfo.uid) {
      userlogin.setlogin(true);
    }

    getuserInfogfun();
  } else {
    userinfo.value = {
      name: user.userInfo.name,
      nickname: user.userInfo.nickname,
      phone: user.userInfo.phone,
      level: user.userInfo.level,
      avatar: user.userInfo.avatar,
    };
    usercode.value = user.userInfo.promo_code;
  }

  nextTick(() => {
    // #ifdef APP-PLUS
    if (!uni.getStorageSync("ysxycode")) {
      popupys.value.open("center");
    }
    // #endif
  });
});
getdata();
async function getuserInfogfun() {
  if (uni.getStorageSync("Authorization")) {
    let res = await user.getuserInfo();
    usercode.value = res.promo_code;
  } else {
    usercode.value = "";
  }
}

const selectcateid = ref("");
const selectsubid = ref("");
const selectindex = ref(0);
const selecttwoindex = ref(0);
const goodlist = ref([]);
const shopform = ref({
  page: 1,
  limit: 100,
  cates: 0,
});

productlstfun();
async function productlstfun() {
  loding.value = false;
  let res = await productindex();
  loding.value = true;
  goodlist.value = res.data;
}

async function bannerfun() {
  let res = await bannerlist();
  bannerarr.value = res.data;
}

const gogoods = (id) => {
  uni.navigateTo({
    url: `/pages/goods/details/index?id=${id}`,
  });
};

async function getdata() {
  // let res = await myIncome({ date: "2024-06" });
}

async function earnCountfun() {
  // let res = await earnCount();
  // taoll.value = res.data;
  // let reslink = await skip();
  // linkobj.value = reslink.data;
  // ishowlin.value = true;
}

// const user = useUserStore();
const statusBarHeight = ref(0);
const menuButtonInfo = ref(null);
const valuetabbar = ref(0);

// 响应式数据
//兼容小程序
// #ifdef MP-WEIXIN
menuButtonInfo.value = uni.getMenuButtonBoundingClientRect();
// #endif

statusBarHeight.value = uni.getSystemInfoSync().statusBarHeight;

// 前往我的客户
const goClientList = () => {
  uni.navigateTo({
    url: "/pages/client/Index",
  });
};

// 前往我的收入
const goIncomeStatistics = () => {
  uni.navigateTo({
    url: "/pages/income-statistics/Index",
  });
};

// 前往推广码
const goProCotionCode = () => {
  uni.navigateTo({
    url: "/pages/promotion-code/Index",
  });
};

// 前往客服
const goContactUs = () => {
  uni.navigateTo({
    url: "/pages/contact-us/Index",
  });
};

// 前往历史业绩
const goIncomeList = () => {
  uni.navigateTo({
    url: "/pages/income-list/Index",
  });
};
//前往我的余额
const goYueEList = () => {
  uni.navigateTo({
    url: "/pages/servicereward/myreward/index",
  });
};

//前往套餐
const goThali = () => {
  uni.navigateTo({
    url: "/pages/thali/upgrade/index",
  });
};
const shareindex = () => {};

// appid="wxbbb8ed3d5b5ada72"
//             username="gh_d08f4ffdf5cc"
//             path="/pages/index/index"

function wxnurl(params, versions) {
  let paramsurl = params ? params : "pages/index/index";
  return `<wx-open-launch-weapp  class="launch-weapp" id="launch-btn" appid="wxbbb8ed3d5b5ada72" username="gh_d08f4ffdf5cc"  path="${paramsurl}" style=" z-index: 99;position: absolute;top: 0; left: 0;display: block;width: 100%;height: 100%;opacity:0;overflow: hidden;" > \<script  type="text/wxtag-template" style="display: block; height: 170px; width: 100%; z-index: 99;opacity:0;"\>\<button style="height: 170px; width: 100%; z-index: 99; display: block"\>点击跳转小程序\<\/button\>\<\/script\></wx-open-launch-weapp>`;
}

const opwx = (url) => {
  let sweixin = null;
  plus.share.getServices(
    (res) => {
      sweixin = res.find((i) => i.id === "weixin");
      if (sweixin) {
        // 分享跳转到微信小程序
        sweixin.launchMiniProgram(
          {
            id: "gh_d08f4ffdf5cc", // 	关联微信小程序的原始ID（"g_"开头的字符串）
            path: url, //	打开小程序的页面路径，不传默认跳转首页
            type: 0, // 	微信小程序版本类型，可取值： 0-正式版； 1-测试版； 2-体验版。 默认值为0。
          },
          (res) => {},
          (err) => {}
        );
      } else {
        // 没有获取到微信分享服务
      }
    },
    (err) => {
      // 获取分享服务列表失败
    }
  );
};

// banner
const bannerconfig = ref({
  indicatorDots: false,
  autoplay: true,
  interval: 2000,
  duration: 500,
  indicatorcolor: "#C3C0BD",
  activecolor: "#ffffff",
});
const taglist = ref([
  {
    id: 1,
    name: "项目介绍",
    icon: imageurl,
    url: "/pages/index/list?code=start_join",
  },
  {
    id: 2,
    name: "加入我们",
    icon: imageur2,
    url: "/pages/index/list?code=extension_tool",
  },
  {
    id: 3,
    name: "推广素材",
    icon: imageur3,
    url: "/pages/index/list?code=product_know",
  },
  {
    id: 4,
    name: "关于我们",
    icon: imageur4,
    url: "/pages/index/list?code=company_profile",
  },
]);

const scrollhei = ref(0);

onMounted(() => {
  nextTick(() => {
    uni
      .createSelectorQuery()
      .select(".home-content")
      .boundingClientRect(function (rect) {
        scrollhei.value = rect.height;
      })
      .exec();
  });
});

const goapayupgrade = () => {
  uni.navigateTo({
    url: "/pages/upgrade/identitylv",
  });
};

const gotagList = (item) => {
  uni.navigateTo({
    url: item.url,
  });
};

const gomorefun = (id) => {
  cates.setcates(id);

  uni.switchTab({
    url: "/pages/goodscate/index",
  });
};
const popupys = ref(null);
const handleDisagree = () => {
  uni.setStorageSync("ysxycode", true);
  popupys.value.close();
};
const handleAgree = () => {
  uni.setStorageSync("ysxycode", true);
  popupys.value.close();
};
const usercode = ref("");
// #ifdef MP-WEIXIN

onShareAppMessage(() => {
  return {
    title: "私语清韵",
    path: "/pages/index/index?referrer_id=" + usercode.value,
    imageUrl:
      "https://m-cloud.yibaitao.com/4b/0fdbe99fed3586dcde22432ac1839b.png",
  };
});

// 朋友圈分享
onShareTimeline(() => {
  return {
    title: "私语清韵",
    path: "/pages/index/index?referrer_id=" + usercode.value,
    imageUr:
      "https://m-cloud.yibaitao.com/4b/0fdbe99fed3586dcde22432ac1839b.png",
  };
});
// #endif
</script>

<template>
  <!-- <up-skeleton :loading="true" :animate="false" avatar rows="2"></up-skeleton> -->
  <view class="my-content">
    <view class="home-banner" ref="banner">
      <swiper
        class="swiper"
        circular
        :indicator-dots="bannerconfig.indicatorDots"
        :autoplay="bannerconfig.autoplay"
        :interval="bannerconfig.interval"
        :duration="bannerconfig.duration"
        :indicator-color="bannerconfig.indicatorcolor"
        :indicator-active-color="bannerconfig.activecolor"
      >
        <swiper-item
          class="item-banner"
          v-for="(item, index) in bannerarr"
          :key="index"
        >
          <view class="item-banner-view">
            <image :src="item.content" mode="widthFix" />
          </view>
        </swiper-item>
      </swiper>
    </view>

    <view class="home-content" ref="content">
      <view class="home-tag-list home-tag-list-3">
        <view
          class="home-tag-list-item"
          v-for="item in taglist"
          :key="item.id"
          @click="gotagList(item)"
        >
          <view class="home-tag-item-img">
            <image :src="item.icon" mode="scaleToFill" />
          </view>
          <view class="home-tag-item-title">{{ item.name }}</view>
        </view>
      </view>

      <view class="home-store" @click="goapayupgrade()">
        <view class="home-title-top"
          ><image src="../../static/my/lv.png" mode="" /><text
            >成为经销商 掌握无限商业</text
          ></view
        >
        <image src="../../static/home/icon-2.png" mode="widthFix" />
      </view>
    </view>

    <!-- <view class="heng-xian"></view> -->

    <view v-for="(item, index) in goodlist" :key="index" class="index-list-k">
      <view class="home-banner-top" v-if="item.product.length > 0">
        <view class="home-banner-title">
          <view class="home-banner-title-left"> {{ item.name }} </view>
          <view class="home-banner-title-right" @click="gomorefun(item.id)">
            <text>更多</text>
            <image src="../../static/index/home_more.png" mode="scaleToFill" />
          </view>
        </view>
        <image src="../../static/index/home-top.png" mode="scaleToFill" />
      </view>

      <view class="home-goods">
        <view class="goods-list">
          <view
            class="goods-item"
            v-for="(good, index) in item.product"
            :key="index"
            @click="gogoods(good.product_id)"
          >
            <view class="goods-image">
              <image :src="good.image" mode="aspectFill" />
            </view>
            <view class="goods-titles"> {{ good.store_name }} </view>

            <view class="goods-price">
              <text>¥{{ good.price }}</text>

              <!-- good.vip_price<text>¥{{ good.price }}</text> -->
            </view>
          </view>
        </view>
      </view>
    </view>

    <!-- <view class="home-banner-top">
      <view class="home-banner-title">
        <view class="home-banner-title-left"> 个护产品专区 </view>
        <view class="home-banner-title-right">
          <text>更多</text>
          <image src="../../static/index/home_more.png" mode="scaleToFill" />
        </view>
      </view>
      <image src="../../static/index/home-top.png" mode="scaleToFill" />
    </view> -->

    <!-- <view class="home-goods">
      <view class="goods-list">
        <view class="goods-item">
          <view class="goods-image">
            <image src="../../static/index/home-top.png" mode="aspectFill" />
          </view>
          <view class="goods-titles"> 净化抑菌液套（臭氧化油抗菌剂） </view>

          <view class="goods-price">
            <text>¥1,288.00</text><text>¥1,188.00</text>
          </view>
        </view>
        <view class="goods-item">
          <view class="goods-image">
            <image src="../../static/index/home-top.png" mode="scaleToFill" />
          </view>
          <view class="goods-titles"> 净化抑菌液套 </view>

          <view class="goods-price">
            <text>¥1,288.00</text><text>¥1,188.00</text>
          </view>
        </view>
        <view class="goods-item">
          <view class="goods-image">
            <image src="../../static/index/home-top.png" mode="scaleToFill" />
          </view>
          <view class="goods-titles"> 净化抑菌液套（臭氧化油抗菌剂） </view>

          <view class="goods-price">
            <text>¥1,288.00</text><text>¥1,188.00</text>
          </view>
        </view>
      </view>
    </view> -->

    <view class="my-botton-f">零壹聚提供技术支持</view>

    <!-- <view class="my-money">
      <view class="my-money-title">
        <view class="my-money-titlename">我的收入</view>
        <view class="my-money-k" @click="goIncomeStatistics">
          <text>查看</text>
          <image
            src="../../static/index/common_more_gray.png"
            mode="widthFix"
          />
        </view>
      </view>
      <view class="my-money-num">
        <view class="my-money-numitem" @click="gocommonbalancefun(1)">
          <view class="my-money-numitem-num">
            <up-count-to
              :startVal="0"
              :endVal="taoll && taoll.month_earn ? taoll.month_earn : 0"
              separator=","
              :decimals="2"
              :useEasing="true"
              :duration="500"
              fontSize="24"
              color="#D49E3B"
              :bold="true"
            ></up-count-to>
          </view>
          <view class="my-money-numitem-name">
            <text>本月实际收入(元)</text>
          </view>
        </view>

        <view class="my-money-numitem" @click="gocommonbalancefun(3)">
          <view class="my-money-numitem-num">
            <up-count-to
              :startVal="0"
              :endVal="taoll && taoll.total_earn ? taoll.total_earn : 0"
              separator=","
              :decimals="2"
              :useEasing="true"
              :duration="500"
              fontSize="24"
              color="#D49E3B"
              :bold="true"
            ></up-count-to
          ></view>
          <view class="my-money-numitem-name">
            <text>累计收入(分)</text>
          </view>
        </view>
      </view>
    </view> -->

    <!-- <wx-open-launch-weapp
      @click="shareindex"
      id="launch-btn"
      appid="wxbbb8ed3d5b5ada72"
      username="gh_d08f4ffdf5cc"
      path="/pages/index/index"
      style="
        z-index: 99;
        position: absolute;
        top: 0;
        left: 0;
        display: block;
        width: 100px;
        height: 100px;
        background: red;
      "
    >
      <div
        :is="'script'"
        type="text/wxtag-template"
        style="display: block; height: 170px; width: 100%; z-index: 99"
      >
        <button style="height: 170px; width: 100%; z-index: 99; display: block">
          点击跳转小程序
        </button>
      </div>
    </wx-open-launch-weapp> -->
    <!-- <wx-open-launch-weapp
      id="launch-btn"
      class="launch-weapp"
      appid="wx2c119a1393644393"
      username="gh_658bb7bff80b"
      :path="shopurl"
      style="z-index: 99; position: absolute; top: 0; left: 0"
    >
      <view
        v-is="'script'"
        type="text/wxtag-template"
        style="display: block; height: 170px; width: 100%; z-index: 99"
      >
        <button
          style="height: 170px; width: 100%; z-index: 99; line-height: 170rpx"
        >
          点击跳转小程序
        </button>
      </view>
    </wx-open-launch-weapp> -->
  </view>

  <!-- #ifdef MP-WEIXIN || WEB  -->
  <tabbarView :value="valuetabbar"></tabbarView>
  <!-- #endif -->

  <!-- #ifdef APP-PLUS -->

  <uni-popup ref="popupys" :animation="true" :is-mask-click="false">
    <view class="popup_content">
      <view class="popup_title">
        {{ "用户协议与隐私政策" }}
      </view>
      <view class="popup_desc">
        {{ "(1)《用户服务协议》中关于个人设备用户信息收集和使用的说明。" }}
      </view>
      <view class="popup_desc">
        {{
          "(2)《隐私政策》中与第三方SDK类服务商数据共享、相关信息收集和使用说明。"
        }}
      </view>
      <view class="popup_desc">
        {{ "用户协议和隐私政策说明:" }}
      </view>
      <br />
      <view class="popup_urlTitle">
        阅读完整的
        <navigator
          url="/pages/mine/userAgreementDeatils?code=user_agree"
          open-type="navigate"
          hover-class="navigator-hover"
          class="popup_url"
        >
          《用户服务协议》
        </navigator>
        与
        <navigator
          url="/pages/mine/userAgreementDeatils?code=privacy_agree"
          open-type="navigate"
          hover-class="navigator-hover"
          class="popup_url"
        >
          《隐私政策》
        </navigator>
        了解 详细信息
      </view>

      <view class="popup_cz">
        <button
          id="disagree-btn"
          type="default"
          class="weui-btn"
          @click="handleDisagree"
        >
          不同意
        </button>
        <button
          id="agree-btn"
          type="primary"
          open-type="agreePrivacyAuthorization"
          class="weui-btn"
          @click="handleAgree"
        >
          同意并继续
        </button>
      </view>
    </view>
  </uni-popup>
  <!-- #endif -->
</template>

<style lang="scss" scoped>
@import "index.scss";
</style>
